package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.ds.domain.entity.Carousel;
import com.jvyou.app.ds.service.CarouselService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 轮播图数据表(Carousel)表控制层
 *
 * @author 橘柚
 * @since 2025-04-02 08:09:59
 */
@RestController
@RequestMapping("/carousel")
public class CarouselController {

    @Resource
    private CarouselService carouselService;

    /**
     * 获取所有可用的轮播图
     *
     * @return 可用的轮播图
     */
    @GetMapping("/all")
    @SaIgnore
    public Result<List<Carousel>> all() {
        return Result.success(carouselService.all());
    }

    @GetMapping("/list")
    @SaIgnore
    public Result<List<Carousel>> list() {
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Carousel::getSortOrder);
        return Result.success(carouselService.list(queryWrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:carousel:get")
    public Result<Carousel> selectOne(@PathVariable Long id) {
        return Result.success(carouselService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param carousel 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:carousel:insert")
    public Result<Carousel> insert(@RequestBody Carousel carousel) {
        boolean success = carouselService.save(carousel);
        return success ? Result.success(carousel) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    /**
     * 修改数据
     *
     * @param carousel 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("system:carousel:update")
    public Result<Carousel> updateById(@RequestBody Carousel carousel) {
        boolean success = carouselService.updateById(carousel);
        return success ? Result.success(carousel) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:carousel:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = carouselService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

}

